package com.wanhe.base.util;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.TypedArray;
import android.os.Build;
import android.os.Environment;
import android.os.Process;
import android.text.ClipboardManager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import com.orhanobut.logger.Logger;

import java.util.Iterator;
import java.util.List;

/**
 * Created by chendeji on 16-5-11.
 */
public class SystemUtil {
	private static final String TAG = SystemUtil.class.getSimpleName();

	public SystemUtil() {
	}

	public static void exit(Context context, OnAppExit appExit) {
		if (appExit != null) {
			appExit.exit();
		}

		Process.killProcess(Process.myPid());
	}

	public static boolean isLastActivityInTask(Context context) {
		ActivityManager manager = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List runningTaskInfos = manager.getRunningTasks(1);
		if (runningTaskInfos != null) {
			Iterator i$ = runningTaskInfos.iterator();

			while (i$.hasNext()) {
				ActivityManager.RunningTaskInfo info = (ActivityManager.RunningTaskInfo) i$.next();
				String packageName = info.topActivity.getPackageName();
				if (context.getPackageName().equalsIgnoreCase(packageName)) {
					int count = info.numActivities;
					if (count == 1) {
						return true;
					}
				}
			}
		}

		return false;
	}

	public static String getAppTopActivity(Context context) {
		ActivityManager manager = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List tasks = manager.getRunningTasks(30);
		if (ObjectUtil.checkObjectList(tasks)) {
			Iterator i$ = tasks.iterator();
			while (i$.hasNext()) {
				ActivityManager.RunningTaskInfo info = (ActivityManager.RunningTaskInfo) i$.next();
				Logger.i(TAG, info.topActivity.getClassName());
				if (info.topActivity.getPackageName().equals(context.getPackageName())) {
					return info.topActivity.getClassName();
				}
			}
		}

		return null;
	}

	public static boolean isAppInstalled(Context context, String packageName) {
		if (packageName != null && !"".equals(packageName)) {
			try {
				ApplicationInfo e = context.getPackageManager().getApplicationInfo(packageName,
						PackageManager.GET_UNINSTALLED_PACKAGES);
				return true;
			} catch (PackageManager.NameNotFoundException var3) {
				return false;
			}
		} else {
			return false;
		}
	}

	public static int getActionBarSize(Context context) {
		TypedValue typedValue = new TypedValue();
		int[] textSizeAttr = new int[]{android.R.attr.actionBarSize};
		byte indexOfAttrTextSize = 0;
		TypedArray a = context.obtainStyledAttributes(typedValue.data, textSizeAttr);
		int actionBarSize = a.getDimensionPixelSize(indexOfAttrTextSize, -1);
		a.recycle();
		return actionBarSize;
	}

	public static boolean hasSdcard() {
		String state = Environment.getExternalStorageState();
		return state.equals("mounted");
	}

	public static void copy(Context context, String text) {
		ClipboardManager cmb = (ClipboardManager) context
				.getSystemService(Context.CLIPBOARD_SERVICE);
		cmb.setText(text);
	}

	public static String getMobleModel() {
		return Build.MODEL;
	}

	public static String getOSVersion() {
		return Build.VERSION.RELEASE;
	}

	public static String getSDKVersion() {
		return String.valueOf(Build.VERSION.SDK_INT);
	}

	public static void toggleSoftInput(Context context) {
		InputMethodManager imm = (InputMethodManager) context
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.toggleSoftInput(0, 2);
	}

	public static void hideSystemKeyBoard(Context mcontext, View v) {
		InputMethodManager imm = (InputMethodManager) mcontext
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
		// imm.toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS);
	}

	public static String getAppVersionName(Context context) {
		return getPackageInfo(context).versionName;
	}

	private static PackageInfo getPackageInfo(Context context) {
		PackageInfo pi = null;

		try {
			PackageManager e = context.getPackageManager();
			pi = e.getPackageInfo(context.getPackageName(), PackageManager.GET_CONFIGURATIONS);
			return pi;
		} catch (Exception var3) {
			var3.printStackTrace();
			return pi;
		}
	}

	public static float getAutoSize(Context context, float size) {
		DisplayMetrics dm = new DisplayMetrics();
		WindowManager windowManager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		windowManager.getDefaultDisplay().getMetrics(dm);
		float reSize;
		if (dm.density > 1.0F) {
			reSize = size / dm.density;
		} else {
			reSize = size * dm.density;
		}

		return reSize;
	}

	/**
	 * 获取屏幕宽度
	 * 
	 * @return int数组[0]width,[1]height
	 */
	public static int[] getWindowWidthAndHeight(Context context) {
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

		int width = wm.getDefaultDisplay().getWidth();
		int height = wm.getDefaultDisplay().getHeight();
		return new int[]{width, height};
	}

	public static void fullScreen(Activity activity) {
		activity.requestWindowFeature(Window.FEATURE_NO_TITLE);
		// 去掉Activity上面的状态栏
		activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
	}

	public static void keepScreenOn(Activity activity) {
		activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
	}

	public interface OnAppExit {
		void exit();
	}

	public static String getSDCardUrl() {
		return Environment.getExternalStorageDirectory().getPath();
	}

	public static int[] getViewLocation(View view) {
		int[] xy = new int[2];
		view.getLocationOnScreen(xy);
		return xy;
	}

}
